<?php

namespace App\Http\Controllers;
use function GuzzleHttp\Promise\all;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Post;
use App\User;
class ExcelController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('excel.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        dd('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function  import(Request $request){

        DB::beginTransaction();

        try{
            if($request->hasFile('file')){
                $file = $request->file('file');
                $this->return = [
                  'code' => 200,
                  'msg' => '导入成功'
                ];
                $unique_arr = [];
                $x = Excel::selectSheetsByIndex(0)->load($file->getPathname(),function ($reader) use (&$unique_arr){
                    $reader->ignoreEmpty();
                    $reader->first();
                    $rows = $reader->skipRows(1)->toArray();
                    if(count($rows) >0 ){
                        $fileds = array_keys($rows[0]);
                        if(in_array('name',$fileds,true)){
                            $this->return = [
                              'code' => '403',
                              'msg' => ''
                            ];
                        }else{

                        }
                    }
                });
            }
        }catch (\Exception $e){
            return $e;
            DB::rollBack();
            return [
                'code' => '403',
                'msg' => '功能异常'
            ];
        }
    }
    public function export(){
        $cellData = [

            ['id','姓名','年龄'],
            ['10001','张三','19'],
            ['10002','李四','22'],
            ['10003','王五','23'],
            ['10004','赵六','19'],

            ['10005','猴七','22'],

        ];
        Excel::create('学生',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
    }

    public function  test () {
        dd('test');
    }

    public function fields() {
        return view('excel.fields');
    }
}
